package com.spring.boot.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spring.boot.entity.SysMenu;
import com.spring.boot.entity.SysRole;
import com.spring.boot.entity.SysRoleMenu;
import com.spring.boot.response.Result;
import com.spring.boot.service.SysMenuService;
import com.spring.boot.service.SysRoleMenuService;
import com.spring.boot.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.management.AttributeList;
import java.util.ArrayList;
import java.util.List;

@RestController
@PreAuthorize("hasAnyRole('roleOption')")
@RequestMapping("/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService roleMenuService;
    @Autowired
    private SysMenuService menuService;

    @PostMapping("/add")
    public Result<?> save(@RequestBody SysRole role){
        sysRoleService.mySave(role);
        SysRole result = sysRoleService.getById(role.getId());
        return Result.OK("新增成功",result);
    }
    @DeleteMapping("/delete/{id}")
    public Result<?> deleteSysRole(@PathVariable int id){
       sysRoleService.removeById(id);
        return Result.OK("删除成功!", id);
    }

    @PostMapping("/edit")
    public Result<?> updateSysRole(@RequestBody SysRole role){
        String menuIds = role.getMenu();
        String[] menuIdArr = menuIds.split(",");
        roleMenuService.remove(new QueryWrapper<SysRoleMenu>().eq("role_id",role.getId()));
        for (String menuId : menuIdArr) {
            roleMenuService.save(new SysRoleMenu(role.getId(),menuId));
        }
        sysRoleService.updateById(role);
        return Result.OK("编辑成功");
    }


    @GetMapping("/page")
    public Result<?> pageUser(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(name = "search", defaultValue = "") String role){

        Page<SysRole> page = new Page<>(pageNum,pageSize);
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        if (!StrUtil.isEmpty(role)){
            queryWrapper.like("role",role);
        }
        Page<SysRole> pageList = sysRoleService.page(page, queryWrapper);
        List<SysRole> newRecords = new ArrayList<>();

        for (SysRole sysRole : pageList.getRecords()) {
            StringBuilder newMenu = new StringBuilder();
            String[] menuIdArr = sysRole.getMenu().split(",");
            for (String menuId : menuIdArr) {
                if(!StrUtil.isEmpty(menuId)){
                    SysMenu menu = menuService.getById(Integer.parseInt(menuId));
                    newMenu.append(menu.getDescr()).append(",");
                }
            }
            sysRole.setMenu(newMenu.toString());
            newRecords.add(sysRole);
        }
        pageList.setRecords(newRecords);
        return Result.OK(pageList);
    }

    @GetMapping("/list")
    public Result<?> list(){
        List<SysRole> roleList = sysRoleService.list();
        return Result.OK(roleList);
    }
}
